#include <vector>
#include <string>

#include "piece.h"
#include "echiquier.h"
//constructeur

Piece::Piece(string a_nom,int a_couleur, int a_x, int a_y, int a_z, int a_passant): couleur(a_couleur), x(a_x), y(a_y), z(a_z), nom(a_nom), passant(a_passant)
{

}

//utilitaires
void Piece::trace(GLUquadric* params)
{
	if (this->get_nom().compare("Vide") == 0)
	{
		return ;
	}
		else if (this->get_nom().compare("Pion") == 0)
	{
		dessin_pion(this->x,this->y,this->z,this->couleur, params);
	}
		else if (this->get_nom().compare("Cavalier") == 0)
	{
		dessin_cavalier(this->x,this->y,this->z,this->couleur, params);
	}
		else if (this->get_nom().compare("Fou") == 0)
	{
		dessin_fou(this->x,this->y,this->z,this->couleur, params);
	}
		else if (this->get_nom().compare("Tour") == 0)
	{
		dessin_tour(this->x,this->y,this->z,this->couleur, params);
	}
		else if (this->get_nom().compare("Dame") == 0)
	{
		dessin_dame(this->x,this->y,this->z,this->couleur, params);
	}
		else if (this->get_nom().compare("Roi") == 0)
	{
		dessin_roi(this->x,this->y,this->z,this->couleur, params);
	}
}
void Piece::bouge_piece(int xa, int ya, int za)
{
	this->x = xa;
	this->y = ya;
	this->z = za;	
}
string Piece::get_nom()
{
	return this->nom;
}

int Piece::get_couleur()
{
	return this->couleur;
}

int Piece::get_x()
{
	return this->x;
}

int Piece::get_y()
{
	return this->y;
}

int Piece::get_z()
{
	return this->z;
}

void Piece::deplacement (Echiquier *echi, vector<int> *tab)
{	
	int x = this->get_x();
	int y = this->get_y();
	int z = this->get_z();
	int couleur = this->get_couleur();
	
	if (this->get_nom().compare("Pion") == 0)
	{
		if ((y == 1) and (couleur == 1) and echi->est_vide(x,y+1,z) and echi->est_vide(x,y+2,z))
		{
			tab->push_back(x);
			tab->push_back(y+2);
			tab->push_back(z);
		}
		
		if ((couleur == 1) and (y < 7) and echi->est_vide(x,y+1,z))
		{
			tab->push_back(x);
			tab->push_back(y+1);
			tab->push_back(z);	
		}
		
		if (couleur == 1)
		{
			for(int i = -1; i<2;i++)
			{
				for(int j = -1; j<2;j++)
				{
					if ((y < 7) and ((i != 0) or (j != 0)) and (echi->get_couleur(x+i,y+1,z+j) == 0))
					{
						tab->push_back(x+i);
						tab->push_back(y+1);
						tab->push_back(z+j);
					}
				}
			}
		}
		
		if ((y == 6) and (couleur == 0) and echi->est_vide(x,y-1,z) and echi->est_vide(x,y-2,z))
		{
			tab->push_back(x);
			tab->push_back(y-2);
			tab->push_back(z);
		}
		
		if ((couleur == 0) and (y > 0) and echi->est_vide(x,y-1,z))
		{
			tab->push_back(x);
			tab->push_back(y-1);
			tab->push_back(z);	
		}
		
		if (couleur == 0)
		{
			for(int i = -1; i<2;i++)
			{
				for(int j = -1; j<2;j++)
				{
					if ((y > 0) and ((i != 0) or (j != 0)) and (echi->get_couleur(x+i,y-1,z+j) == 1))
					{
						tab->push_back(x+i);
						tab->push_back(y-1);
						tab->push_back(z+j);
					}
				}
			}
		}
	}
	
	if ((this->get_nom().compare("Tour")==0) or (this->get_nom().compare("Dame")==0))
	{
		
		int aux1[]={0,0,0,0,-1,1};
		int aux2[]={0,0,-1,1,0,0};
		int aux3[]={-1,1,0,0,0,0};
	
		int i,j,k;
		
		for(int l = 0; l<6;l++)
		{
			i = x + aux1[l];
			j = y + aux2[l];
			k = z + aux3[l];
		
			while((i>=0) and (i<6) and (j>=0) and (j<8) and (k>=0) and (k<6) and echi->est_vide(i,j,k))
			{
				tab->push_back(i);
				tab->push_back(j);
				tab->push_back(k);
				i += aux1[l];
				j += aux2[l];
				k += aux3[l];
			}
			if((i>=0) and (i<6) and (j>=0) and (j<8) and (k>=0) and (k<6) and (couleur != echi->get_couleur(i,j,k)))
			{
				tab->push_back(i);
				tab->push_back(j);
				tab->push_back(k);
			}
		}
	}

	if ((this->get_nom().compare("Fou")==0) or (this->get_nom().compare("Dame")==0))
	{
		int aux1[] = {-1, -1, 1, 1};
		int aux2[] = {-1, 1, -1, 1};
		
		int i,j,k;
		
		for(int m = -1; m<2; m++)
		{
			for(int l = 0; l < 4; l++)
			{
				i = x + aux1[l];
				j = y + aux2[l];
				k = z + m;
			
				while((i>=0) and (i<6) and (j>=0) and (j<8) and (k>=0) and (k<6) and echi->est_vide(i,j,k))
				{
					tab->push_back(i);
					tab->push_back(j);
					tab->push_back(k);
					i += aux1[l];
					j += aux2[l];
				}
				if((i>=0) and (i<6) and (j>=0) and (j<8) and (k>=0) and (k<6) and (couleur != echi->get_couleur(i,j,k)))
				{
					tab->push_back(i);
					tab->push_back(j);
					tab->push_back(k);
				}
			}
			
			for(int l = 0; l < 4; l++)
			{
				i = x +m ;
				j = y + aux2[l];
				k = z + aux1[l];
			
				while((i>=0) and (i<6) and (j>=0) and (j<8) and (k>=0) and (k<6) and echi->est_vide(i,j,k))
				{
					tab->push_back(i);
					tab->push_back(j);
					tab->push_back(k);
					i += aux1[l];
					j += aux2[l];
				}
				if((i>=0) and (i<6) and (j>=0) and (j<8) and k>=0 and (k<6) and (couleur != echi->get_couleur(i,j,k)))
				{
					tab->push_back(i);
					tab->push_back(j);
					tab->push_back(k);
				}		
			}
		}
	}
	if (this->get_nom().compare("Roi") == 0)
	{
		for(int a = -1; a<2;a++)
		{
			for(int b = -1; b<2; b++)
			{
				for(int c = -1; c<2;c++)
				{
					if((x+a>=0) and (x+a<6) and (y+b>=0) and (y+b<8) and (c+z <6) and (c+z>=0) and ((x!=0 ) or (y!=0) or (z!=0)) and (couleur != echi->get_couleur(x+a,y+b,z+c)))
					{
						tab->push_back(x+a);
						tab->push_back(y+b);
						tab->push_back(z+c);
					}
				}
			}
		}
	}
}
